package com.itheima.app.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice //专门用于捕捉controller的异常
public class GlobalExceptionAdvice {

    //业务异常 如果发生异常此处统一捕捉 统一处理
    @ExceptionHandler(Exception.class)
    public ResponseEntity businessException(Exception ex){
        //返回统一的处理   ex.getMessage(): 后续抛出异常的内容
        // throw new RuntimeException("人脸识别不通过");   人脸识别不通过 == ex.getMessage()

        ex.printStackTrace();
        return ResponseEntity.status(500).body(ex.getMessage());
    }

    //处理自定义的异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerBusinessException(Exception exception) {
        //打印异常
        exception.printStackTrace();

        //返回错误
        Map<String, String> errorResult = new HashMap<>();
        errorResult.put("message", exception.getMessage());//获取自定义异常信息
        return ResponseEntity.status(500).body(errorResult);
    }
}
